<?php
/**
 *
 *
 * @author Zeon <scplay@gmail.com>
 * @date 2018/8/29 11:08
 * @copyright PanxSoft Inc.
 */

namespace App\Services\Presenters\Wechat\TemplateNotice;


use App\Models\Store\HmSkinRecord;
use App\Models\Store\SkinRecord;
use App\Models\Store\WaxSkinRecord;
use App\Services\Presenters\HmSkinData;
use App\Services\Presenters\WaxSkinData;

class SkinRecordNoticePresenter
{
    /**
     * @param SkinRecord $skin_record
     * @param $customer_id
     * @return string
     */
    public function assembleRemark(SkinRecord $skin_record, $customer_id)
    {
        if ($skin_record->skin_detector_id === SkinRecord::WAX) {
            $info = $this->getWaxTplRemarkInfo($skin_record, $customer_id);
        } else {
            $info = $this->getHmTplRemarkInfo($skin_record, $customer_id);
        }

        return $info;
    }

    /**
     * @param SkinRecord $skin_record
     * @param $customer_id
     * @return string
     */
    public function getWaxTplRemarkInfo(SkinRecord $skin_record, $customer_id): string
    {
        $wax = $skin_record->waxSkinRecords()
            ->whereIn('itemsid', WaxSkinRecord::ITEMSID_ALL)
            ->get();

        /** @var WaxSkinRecord $moisture - 额头 */
        $moisture     = $wax->firstWhere('itemsid', WaxSkinRecord::ITEMSID_MOISTURE);
        /** @var WaxSkinRecord $oil - 额头 */
        $oil          = $wax->firstWhere('itemsid', WaxSkinRecord::ITEMSID_OIL);

        /** @var WaxSkinRecord $redblood - 脸颊 */
        $redblood     = $wax->firstWhere('itemsid', WaxSkinRecord::ITEMSID_REDBLOOD);
        /** @var WaxSkinRecord $pigmentation - 脸颊 */
        $pigmentation = $wax->firstWhere('itemsid', WaxSkinRecord::ITEMSID_PIGMENTATION);

        /** @var WaxSkinRecord $acne - 鼻子 */
        $acne         = $wax->firstWhere('itemsid', WaxSkinRecord::ITEMSID_FOLLICLE);

        $forehead = trans('wechat.forehead');
        $cheek = trans('wechat.cheek');
        $nose = trans('wechat.nose');

        $info  = $forehead."：" . $this->formatWaxPosition([$moisture, $oil], $customer_id)          . "\n";
        $info .= $cheek.'：' . $this->formatWaxPosition([$redblood, $pigmentation], $customer_id) . "\n";
        $info .= $nose.'：' . $this->formatWaxPosition([$acne], $customer_id)                    . "\n";

        return $info;
    }

    /**
     * @param WaxSkinRecord[]|array $records
     *
     * @param $customer_id
     * @return string
     */
    private function formatWaxPosition(array $records, $customer_id): string
    {
        $wax_skin_data = WaxSkinData::WAX_SKIN_DATA[WaxSkinData::LANG_ZH];

        return implode(
            "、",
            array_map(
                function (WaxSkinRecord $record) use ($wax_skin_data, $customer_id){
//                    return WaxSkinData::getWaxSkinData($record->itemsid,$record->levelsort)['levelname'];
                    return WaxSkinData::transScoreLevelNameByCustom($record->itemsid, $record->levelsort, $customer_id);
                },
                $records
            )
        );
    }

    /**
     * @param SkinRecord $skin_record
     * @param $customer_id
     * @return string
     */
    public function getHmTplRemarkInfo(SkinRecord $skin_record, $customer_id): string
    {
        $records = $skin_record->hmSkinRecords()
            ->whereIn('position', HmSkinRecord::ALL_POSITIONS)
            ->get();

        /** @var HmSkinRecord $eye - 眼尾 */
        $eye   = $records->firstWhere('position', HmSkinRecord::POSITION_EYE);
        /** @var HmSkinRecord $cheek - 脸颊 */
        $cheek = $records->firstWhere('position', HmSkinRecord::POSITION_CHEEK);
        /** @var HmSkinRecord $neck - 脖子 */
        $neck  = $records->firstWhere('position', HmSkinRecord::POSITION_NECK);
        /** @var HmSkinRecord $hand - 手臂 */
        $hand  = $records->firstWhere('position', HmSkinRecord::POSITION_HAND);

        $eye_tail = trans('wechat.eye_tail');
        $_cheek = trans('wechat.cheek');
        $_neck = trans('wechat.neck');
        $arm = trans('wechat.arm');

        $info  = '';
        $info .= $eye_tail.'：' . $this->formatHmPosition($eye, $customer_id)   . "\n";
        $info .= $_cheek.'：' . $this->formatHmPosition($cheek, $customer_id) . "\n";
        $info .= $_neck.'：' . $this->formatHmPosition($neck, $customer_id)  . "\n";
        $info .= $arm.'：' . $this->formatHmPosition($hand, $customer_id)  . "\n";

        return $info;
    }

    /**
     * @param HmSkinRecord $hm_position
     * @param $customer_id
     * @return string
     */
    private function formatHmPosition(HmSkinRecord $hm_position, $customer_id): string
    {
        $water = HmSkinData::transScoreTitleByCustom(HmSkinData::HM_WATER, $hm_position->water, $customer_id);

        $oil = HmSkinData::transScoreTitleByCustom(HmSkinData::HM_OIL, $hm_position->oil, $customer_id);

        $flex = trans('service.flex') . HmSkinData::transScoreTitleByCustom(HmSkinData::HM_FLEX, $hm_position->flex, $customer_id);

        return implode("、" , [$water, $oil, $flex]);
    }
}